﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Lexer {
	/// <summary>
	/// A pattern that will either match or reject a single character or a repeat of a single character.
	/// </summary>
	public class CharPattern {
		/// <summary>
		/// The character pattern.
		/// </summary>
		public Regex Pattern { get; set; }

		/// <summary>
		/// Whether the character may repeat.
		/// </summary>
		public bool IsRepeating { get; set; }

		public bool IsNegated { get; set; }

		public CharPattern(Regex pattern, bool negated) {
			Pattern = pattern;
			IsNegated = negated;
		}

		public override bool Equals(object obj) {
			return obj.GetType().Equals(typeof(CharPattern)) &&
				IsNegated == (obj as CharPattern).IsNegated &&
				Pattern.ToString().Equals((obj as CharPattern).Pattern.ToString());
		}

		public override int GetHashCode() {
			return Pattern.ToString().GetHashCode() + (IsNegated ? 1 : 0);
		}

		public bool matches(string s) {
			return Pattern.IsMatch(s) ^ IsNegated;
		}

		public override string ToString() {
			return (IsNegated ? "!" : "") +
				Pattern.ToString() +
				(IsRepeating ? "+" : "");
		}
	}
}
